package com.qubuyer.business.auth.view

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import com.qubuyer.R
import com.qubuyer.business.auth.viewmodel.ForgetPwdViewModel
import com.qubuyer.core.app.MVVMFragment
import com.qubuyer.databinding.FragmentForgetPwdBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

class ForgetPwdFragment : MVVMFragment<FragmentForgetPwdBinding, ForgetPwdViewModel>() {

    override val viewModel: ForgetPwdViewModel by injectVMByActivity()

    override fun onDataBinding(dataBinding: FragmentForgetPwdBinding) {
        dataBinding.host = this
    }

    override val layoutId: Int
        get() = R.layout.fragment_forget_pwd

    private var inCountDown = false

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewDataBinding.etMobile.doAfterTextChanged {
            viewDataBinding.ivDelMobile.isVisible = !it.isNullOrEmpty()
            viewDataBinding.tvGetCode.isEnabled = it?.length == 11 && !inCountDown
            checkInput()
        }
        viewDataBinding.etCode.doAfterTextChanged {
            viewDataBinding.ivDelCode.isVisible = !it.isNullOrEmpty()
            checkInput()
        }
        viewDataBinding.etPwd.doAfterTextChanged {
            viewDataBinding.ivDelPwd.isVisible = !it.isNullOrEmpty()
            checkInput()
        }
        viewDataBinding.etPwdSecond.doAfterTextChanged {
            viewDataBinding.ivDelPwdSecond.isVisible = !it.isNullOrEmpty()
            checkInput()
        }
        viewModel.getCodeResult.observe(viewLifecycleOwner, {
            if (it) {
                toast("验证码获取成功")
                viewDataBinding.tvGetCode.isEnabled = false
                launch {
                    (60 downTo 0).asFlow().onEach { index ->
                        inCountDown = true
                        viewDataBinding.tvGetCode.text = "重新获取${index}S"
                        delay(1000)
                    }.collectLatest {
                        inCountDown = false
                        viewDataBinding.tvGetCode.text = "获取验证码"
                        viewDataBinding.tvGetCode.isEnabled = false
                    }
                }
            }
        })
        viewModel.result.observe(viewLifecycleOwner, {
            when (it) {
                200 -> {
                    toast("密码修改成功")
                    mContext.onBackPressed()
                }
            }
        })
    }

    override fun getTitle(): String {
        return "忘记密码"
    }

    fun onClick(v: View) {
        when (v.id) {
            R.id.ivDelMobile -> viewDataBinding.etMobile.setText("")
            R.id.ivDelCode -> viewDataBinding.etCode.setText("")
            R.id.ivDelPwd -> viewDataBinding.etPwd.setText("")
            R.id.ivDelPwdSecond -> viewDataBinding.etPwdSecond.setText("")
            R.id.tvConfirm -> {
                val phone = viewDataBinding.etMobile.text.toString()
                val code = viewDataBinding.etCode.text.toString()
                val pwd = viewDataBinding.etPwd.text.toString()
                val pwd1 = viewDataBinding.etPwdSecond.text.toString()
                viewModel.findPwd(phone, code, pwd, pwd1)
            }
            R.id.tvGetCode -> {
                val phone = viewDataBinding.etMobile.text.toString()
                viewModel.getCode(phone)
            }
        }
    }

    private fun checkInput() {
        viewDataBinding.tvConfirm.isEnabled = viewDataBinding.etMobile.length() == 11 &&
                viewDataBinding.etCode.length() == 6 &&
                viewDataBinding.etPwd.length() >= 3 &&
                viewDataBinding.etPwdSecond.text.toString() == viewDataBinding.etPwd.text.toString()
    }
}